import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

import 'package:get/get.dart';
import 'package:myapp/app/modules/info/views/info_view.dart';
import 'package:myapp/app/modules/movie/views/movie_view.dart';
import 'package:myapp/app/modules/profile/views/profile_view.dart';
import 'package:myapp/app/theme/app_color.dart';
import 'package:myapp/app/theme/resource/images.dart';
import '../controllers/home_controller.dart';
import 'bottom_navigation_bar_item.dart';

class HomeView extends GetView<HomeController> {
  final _bottomBarItemList = <BottomNavigationBarItem>[
    BottomBarItem(
        url: ImageKeys.bar_info,
        activeUrl: ImageKeys.bar_info_rounded,
        label: "首页"),
    BottomBarItem(
        url: ImageKeys.bar_movie,
        activeUrl: ImageKeys.bar_movie_rounded,
        label: "放映厅"),
    BottomBarItem(
        url: ImageKeys.bar_profile,
        activeUrl: ImageKeys.bar_profile_rounded,
        label: "我的")
  ];

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      child: Scaffold(
        body: PageView(
          children: [
            InfoView(),
            MovieView(),
            ProfileView(),
          ],
          controller: controller.pageController,
          physics: NeverScrollableScrollPhysics(),
        ),
        bottomNavigationBar: Obx(
          () => BottomNavigationBar(
            items: _bottomBarItemList,
            selectedItemColor: AppColor.red,
            unselectedItemColor: AppColor.hex_333,
            selectedFontSize: 12,
            unselectedFontSize: 12,
            currentIndex: controller.currentIndex.value,
            onTap: (index) => controller.changeIndex(index),
          ),
        ),
      ),
      onWillPop: _isExit,
    );
  }

  DateTime? _lastTime;

  Future<bool> _isExit() {
    if (_lastTime == null ||
        DateTime.now().difference(_lastTime!) > Duration(milliseconds: 2500)) {
      _lastTime = DateTime.now();
      Fluttertoast.showToast(msg: "再按一次退出");
      return Future.value(false);
    }
    return Future.value(true);
  }
}
